﻿using System.Collections.Generic;
using DBFile.Core.Impl;
using DBFile.Core.Interfaces;
using DBFile.Providers.Wcf;

namespace DBFile.Providers.Impl
{
    public class WcfClient : IDBFileProvider
    {
        private string endpointConfigurationName;

        public WcfClient(string endpointConfigurationName)
        {
            this.endpointConfigurationName = endpointConfigurationName;
        }

        public bool Exists(string filename)
        {
            bool exists;
            using (FileProviderClient client = new FileProviderClient(endpointConfigurationName))
            {
                exists = client.Exists(filename);
                client.Close();
            }

            return exists;
        }

        public int Copy(string sourceFilename, string destinationFilename, bool overwrite)
        {
            int results;
            using (FileProviderClient client = new FileProviderClient(endpointConfigurationName))
            {
                results = client.Copy(sourceFilename, destinationFilename, overwrite);
                client.Close();
            }
            return results;
        }

        public int Insert(string filename, byte[] buffer, string contentType, long contentLength)
        {
            int results;
            using (FileProviderClient client = new FileProviderClient(endpointConfigurationName))
            {
                results = client.Insert(filename, buffer, contentType, contentLength, false);
                client.Close();
            }
            return results;
        }

        public int Insert(string filename, byte[] buffer, string contentType, long contentLength, bool overwrite)
        {
            int results;
            using (FileProviderClient client = new FileProviderClient(endpointConfigurationName))
            {
                results = client.Insert(filename, buffer, contentType, contentLength, overwrite);
                client.Close();
            }
            return results;
        }

        public int Update(string filename, byte[] buffer, string contentType, long contentLength)
        {
            int results;
            using (FileProviderClient client = new FileProviderClient(endpointConfigurationName))
            {
                results = client.Update(filename, buffer, contentType, contentLength);
                client.Close();
            }
            return results;
        }

        public int Delete(string filename)
        {
            int results;
            using (FileProviderClient client = new FileProviderClient(endpointConfigurationName))
            {
                results = client.Delete(filename);
                client.Close();
            }
            return results;
        }

        public string[] GetFileNameList()
        {
            List<string> results = new List<string>();

            using (FileProviderClient client = new FileProviderClient(endpointConfigurationName))
            {
                results.AddRange(client.GetFileNameList());
                client.Close();
            }
            return results.ToArray();
        }

        public T Get<T>(string filename) where T : IDBFile, new()
        {
            using (FileProviderClient client = new FileProviderClient(endpointConfigurationName))
            {
                if (client.Exists(filename))
                {
                    DBFileInfo info = client.Get(filename);
                    client.Close();

                    return info.ConvertTo<T>(); ;
                }
            }
            return default(T);
        }
    }
}
